package com.example.weight.greenSeasons.waterDeal;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.navigation.SijiqingFlocculationNavActivity;
import com.example.weight.wormHandle.cultivate.SmallWormBreedingIncoming;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class EquipmentRunningTable extends AppCompatActivity {

    private TitleBar titleBar;
    private Button saveButton;
    private TextView dateTextView;
    private EditText etPressure1, etPressure2, etPressure3, etFrequency1, etFrequency2, etRemarks;
    private Integer pressure1, pressure2, pressure3, frequency1, frequency2, frequency3;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
    private String factory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent previousIntent = getIntent();
        factory = previousIntent.getStringExtra("factory");

        setContentView(R.layout.activity_equipment_running_table);

        titleBar=findViewById(R.id.titleBar);
        saveButton = findViewById(R.id.btnSave);
        dateTextView = findViewById(R.id.equipment_running_date);
        etPressure1 = findViewById(R.id.et_pressure1);
        etPressure2 = findViewById(R.id.et_pressure2);
        etPressure3 = findViewById(R.id.et_pressure3);
        etFrequency1 = findViewById(R.id.et_frequency1);
        etFrequency2 = findViewById(R.id.et_frequency2);
        etRemarks = findViewById(R.id.et_remarks);

        dateTextView.setText(simpleDateFormat.format(new Date()));

        titleBar.setOnTitleBarListener(new OnTitleBarListener() {

            @Override
            public void onLeftClick(View v) {
                EquipmentRunningTable.this.finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }


        });
    }

    public void save(View view) {
        if (etPressure1 == null) {
            Toast.makeText(this, "请填写膜泵压力1", Toast.LENGTH_SHORT).show();
            return;
        }
        if(etPressure2.getText().toString().equals("")) {
            Toast.makeText(this, "请填写膜泵压力2", Toast.LENGTH_SHORT).show();
            return;
        }
        if(etPressure3.getText().toString().equals("")) {
            Toast.makeText(this, "请填写膜泵压力3", Toast.LENGTH_SHORT).show();
            return;
        }
        if(etFrequency1.getText().toString().equals("")) {
            Toast.makeText(this, "请填写池风机频率1", Toast.LENGTH_SHORT).show();
            return;
        }
        if(etFrequency2.getText().toString().equals("")) {
            Toast.makeText(this, "请填写池风机频率2", Toast.LENGTH_SHORT).show();
            return;
        }
//        if(etRemarks.getText().toString().equals("")) {
//            Toast.makeText(this, "请填写备注", Toast.LENGTH_SHORT).show();
//            return;
//        }

        new AlertDialog.Builder(this)
                .setTitle("确定保存吗？")
                .setCancelable(false)
                .setNegativeButton("取消", ((dialog, which) ->
                        Toast.makeText(this, "取消保存", Toast.LENGTH_SHORT).show()))
                .setPositiveButton("确定", (dialog, which) -> {
                    //提交请求
                    RequestQueue queue = Volley.newRequestQueue(this);
                    String url = Config.url + "/sjq/SjqWaterProductionEquipmentRunning/addForm";
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String code = jsonObject.getString("code");
                                        if (code.startsWith("2")){
                                            Toast.makeText(EquipmentRunningTable.this,
                                                    "保存成功", Toast.LENGTH_SHORT).show();
//                                            Intent intent = new Intent(EquipmentRunningTable.this,SmallWormBreedingIncoming.class);
//                                            startActivity(intent);
//                                            EquipmentRunningTable.this.finish();
                                        }
                                        else
                                            Toast.makeText(EquipmentRunningTable.this,
                                                    "保存失败", Toast.LENGTH_SHORT).show();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(EquipmentRunningTable.this, "保存失败", Toast.LENGTH_SHORT).show();
                            Log.d("LB", error.toString());
                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> map=new HashMap<>();
                            map.put("pump_pressure_1", etPressure1.getText().toString());
                            map.put("pump_pressure_2", etPressure2.getText().toString());
                            map.put("pump_pressure_3", etPressure3.getText().toString());
                            map.put("fan_frequency_1", etFrequency1.getText().toString());
                            map.put("fan_frequency_2", etFrequency2.getText().toString());
                            map.put("operator", getIntent().getStringExtra("name"));
//                            map.put("remarks", etRemarks.getText().toString());
                            map.put("factory", factory);
                            return map;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    queue.add(stringRequest);
                })
                .show();
    }
}